/*
 * ServerInformation.cpp
 *
 *  Created on: Dec 5, 2014
 *      Author: toti
 */

#include <Misc/ServerInformation.hpp>


ServerInformation::ServerInformation(IBRCNetworkManager *ibrcNetworkManager){

	this->ibrcNetworkManager = ibrcNetworkManager;

	fileName = Config::instance()->getServerInformationFile();

}



void ServerInformation::writeServerInformationFile(){

	std::ofstream ofs(fileName);

	ofs << "Server Information file:" << std::endl << std::endl;


	/*
	 * Server list
	 */
	ofs << "Network server [" << ibrcNetworkManager->getNetworkServerList()->size() << "]" << std::endl;
	ofs << "\t[SERVER NAME]" << std::endl;
	std::list<NetworkServer*> *networkServerList = ibrcNetworkManager->getNetworkServerList();
	for(std::list<NetworkServer*>::iterator it=networkServerList->begin();it!=networkServerList->end();++it){

		NetworkServer *networkServer = *it;

		ofs << "\t" << networkServer->getServerName() << std::endl;

	}

	ofs << std::endl;


	/*
	 * Client list
	 */
	ofs << "Network clients [" << ibrcNetworkManager->getNetworkClientList()->size() << "]" << std::endl;
	ofs << "\t[CLIENT NAME],\t[CHANNEL NAME]" << std::endl;

	std::list<NetworkClient*> *networkClientList = ibrcNetworkManager->getNetworkClientList();
	for(std::list<NetworkClient*>::iterator it=networkClientList->begin();it!=networkClientList->end();++it){

		NetworkClient *networkClient = *it;
		NetworkChannel *networkChannel = networkClient->getNetworkChannel();

		ofs << "\t" << networkClient->getClientName() << ",\t";

		if(networkChannel){
			ofs << networkChannel->getChannelName();
		}else{
			ofs << "none";
		}

		ofs << std::endl;
	}

	ofs << std::endl;


	/*
	 * Channel list
	 */
	ofs << "Network channels [" << ibrcNetworkManager->getNetworkChannelList()->size() << "]" << std::endl;
	ofs << "\t[CHANNEL NAME],\t[CHANNEL OWNER],\t[CHANNEL TOPIC]" << std::endl;

	std::list<NetworkChannel*> *networkChannelList = ibrcNetworkManager->getNetworkChannelList();
	for(std::list<NetworkChannel*>::iterator it=networkChannelList->begin();it!=networkChannelList->end();++it){

		NetworkChannel *networkChannel = *it;
		NetworkClient *networkClient = networkChannel->getChannelOwner();

		ofs << "\t" << networkChannel->getChannelName() << ",\t";

		if(networkClient){
			ofs << networkClient->getClientName();
		}else{
			ofs << "none";
		}

		ofs << ",\t" << networkChannel->getTopic();

		ofs << std::endl;

		for(std::list<NetworkClient*>::iterator it=networkClientList->begin();it!=networkClientList->end();++it){

			if(networkChannel == (*it)->getNetworkChannel()){
				ofs << "\t\t" << (*it)->getClientName() << std::endl;
			}

		}

	}

	ofs.flush();

}


